﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class RicePricesRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<RicePrice> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.RicePrices
                          select p;
            totalRow = results.Count();
            return results.OrderBy(p=>p.RiceID).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public RicePrice GetByID(int ID)
        {
            var result = from p in data.RicePrices
                         where p.RiceID == ID
                         select p;
            return result.FirstOrDefault();
        }

        public void Insert(RicePrice rp)
        {
            data.AddToRicePrices(rp);
        }

        public void DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null)
            {
                data.DeleteObject(obj);
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }
    }
}